<template>
<div class="whole-main" :class="isShow ? 'bg_red' : ''">
  <div class="convert-whole" v-if="isShow">
    <img src="https://img.wifenxiao.com/h5-wfx/images/convert/convertIcons.png" class="convert-bg">
          <!-- TODO 小程序文字滚动 -->
    <div class="notice-bar" v-if="notice">
      <p class="lunbo-p" :style="{transform: 'translateX(-'+scrollLeft+'px)', marginLeft:  marginLeft + 'px'}">{{notice}}</p>
      <span class="notice-icon"></span>
    </div>
    <div class="convent-search">
      <div class="title">输入兑换订单号</div>
      <div class="covert-input">
        <input type="text" v-model="order" :placeholder="prompt">
      </div>
      <div class="convert-btn" @click="onConvent()">立即兑换</div>
      <div class="my-convert" @click="jump('convertOrderList')">
        <img src="https://img.wifenxiao.com/h5-wfx/images/convert/report.png">
        <span>我的兑换记录</span>
      </div>
    </div>
    <div class="convent-search" v-if="description">
      <div class="title">订单转换说明</div>
      <div class="convert-main">
        <div v-html="description"></div>
      </div>
    </div>
    <van-popup v-model="confirmConvert" closeable class="confirm-whole">
      <div class="convent-search">
        <div class="title">即将兑换订单</div>
        <div class="list">
          <div>订单编号：{{orderDetail.order_no}}</div>
          <div>订单状态：{{orderDetail.status_name}}</div>
          <div>平台来源：{{orderDetail.platform}}</div>
          <div>订单金额：￥{{orderDetail.payment}}</div>
        </div>
        <div class="reward">
          <div class="title">{{orderDetail.status == 3 ? '可转换奖励' : '可转换冻结奖励'}}</div>
          <div class="tips">{{orderDetail.status == 3 ? '转换后可立即收到' : '订单确认收货完结后可领取奖励，退款将不会发放'}}</div>
          <div class="reward-list">
            <div class="point"><img src="https://img.wifenxiao.com/h5-wfx/images/convert/point.png" />{{orderDetail.point}}积分</div>
            <div class="money"><img src="https://img.wifenxiao.com/h5-wfx/images/convert/money.png" />{{orderDetail.balance}}元</div>
          </div>
          <div class="convert-btn" @click="submitConvert">确认并转换</div>
        </div>

      </div>
    </van-popup>
  </div>
  <div v-if="isShow == 0">
      <no-data :noData="noData"></no-data>
  </div>
</div>

</template>
<script>
import Vue from 'vue'
import { NoticeBar, Popup } from 'vant'
import noData from '@/components/noData/index'
import { getConvertConfig, getConvertUserOrder, convertConfirm } from '@/api/convent/conventApi.js'
import small from '@/smallapp/small'

Vue.use(NoticeBar).use(Popup)
export default {
  components: {
    noData
  },
  data() {
    return {
      order: '',
      confirmConvert: false,
      isShow: false,
      noData: {
        img: '',
        text: '功能暂未开启，敬请期待',
        marginTop: '60px'
        // width: '214px',
        // txtMarginTop: '-46px'
      },
      description: '', // 说明
      prompt: '', // placeholder
      notice: '', // 公告
      orderDetail: {
        order_no: '', // 订单编号
        status: '', // 状态值
        status_name: '', // 状态名称
        platform: '', // 平台
        payment: 0, // 实付款
        point: 0, // 积分
        balance: 0 // 余额
      },
      scrollLeft: 0,
      timer: null,
      contentWidth: 0,
      wrapWidth: 0,
      marginLeft: 40
    }
  },
  created() {
      wx.setNavigationBarTitle({
        title: '订单转换'
      })
    this.isShow = small.wxCache('isOrderConvert')
    this.notice = small.wxCache('orderConvertNotice')
    if (this.isShow) {
      this.$loadingWrap.show()
      getConvertConfig().then(res => {
        this.$loadingWrap.close()
        if (res.status == 1) {
          this.prompt = res.data.prompt
          this.description = res.data.description
          // this.notice = res.data.notice
          wx.setNavigationBarTitle({
            title: res.data.order_convert_name
          })
          this.handleScrollLeft()
        } else {
          this.$Error(res.msg)
        }
      })
    }
  },
  destroyed() {
    this.timer = null
    clearInterval(this.timer)
    this.left = 0
  },
  methods: {
    handleScrollLeft() {
      const title = this.notice
      this.wrapWidth = (title.length * 25) / 2
      // console.log(this.wrapWidth, this.notice, 'this.notice')
      document.querySelector('.lunbo-p').$$getBoundingClientRect().then((res) => {
        this.contentWidth = res.width - this.marginLeft
        this.marginLeft = (res.width - this.marginLeft) / 10

      })
      if (!this.timer) {
        this.timer = setInterval(() => {
          if (this.contentWidth >= this.wrapWidth) {
            clearInterval(this.timer)
            this.marginLeft = 46
          } else if (this.scrollLeft < this.wrapWidth + this.contentWidth) {
            this.scrollLeft++
          } else {
            this.scrollLeft = 0
          }
        }, 30)
      }
    },
    jump(url, data) {
      this.$JumpName(this, url, data)
    },
    onConvent() {
      if (this.order) {
        this.$loadingWrap.show()
        getConvertUserOrder({ order_no: this.order }).then(res => {
          this.$loadingWrap.close()
          if (res.status == 1) {
            this.confirmConvert = true
            this.orderDetail = res.data.data
          } else {
            this.$Error(res.msg)
          }
        })
      } else {
        this.$Error('请输入兑换订单号')
      }
    },
    submitConvert() {
      this.confirmConvert = false
      this.$loadingWrap.show()
      convertConfirm({ order_no: this.order }).then(res => {
        this.$loadingWrap.close()
        if (res.status == 1) {
          // this.orderDetail = res.data
          this.$Error('转换成功')
          setTimeout(() => {
            this.jump('convertOrderList')
          }, 1000)
        } else {
          this.$Error(res.msg)
        }
      })
    }
  },
}
</script>
<style lang="scss">
.app-wrapper {
  padding-bottom: 0;
}
.whole-main {
  padding-top: 24px;
  padding-bottom: 30px;
  width:100vw;
	min-height:100vh; 
  &.bg_red {
    background: linear-gradient(180deg, #F62430 46.21%, #F7493E 89.02%, #F8A964 201.22%);
  }
}
.convert-whole {
  position: relative;
  .convert-bg {
    position: absolute;
    top: -24px;
    left: 0;
    width: 100%;
  }
  .notice-bar {
    position: relative;
    top: -24px;
    font-size: 24px;
    position: relative;
    width: 100%;
    height: 66px;
    background: #C71000;
    overflow: hidden;
    box-sizing: border-box!important;
    .notice-icon{
      position: absolute;
      left: 0px;
      top: 0;
      width: 64px;
      height: 66px;
      background: #C71000 url(https://img.wifenxiao.com/h5-wfx/images/convert/notice.png) no-repeat right center;
      background-size: 32px 32px;
    }
    .lunbo-p {
      width: 100%;
      line-height: 66px;
      display: inline-block;
      white-space: nowrap;
      color: #fff;
    }
  }
  .convent-search {
    border-radius: 16px;
    background-color: #fff;
    // height: 440px;
    margin: 0 24px 24px 24px;
    position: relative;
    padding: 48px 32px;
    .title {
      font-size: 32px;
      font-weight: 500;
      text-align: center;
      line-height: 140%;
    }
    .covert-input {
      padding: 40px 0;
      input {
        border-radius: 8px;
        padding: 14px 32px;
        border: 1px solid #DCDFE6;
        width: 100%;
        font-size: 40px 32px;
      }
    }
    .convert-btn {
      background-color: #FF4332;
      border-radius: 200px;
      padding: 16px 32px;
      border: 1px solid #FF4332;
      font-size: 32px;
      color: #fff;
      text-align: center;
    }
    .my-convert {
      text-align: center;
      margin-top: 40px;
      img {
        width: 32px;
        height: 32px;
        position: relative;
        top: 6px;
      }
      span {
        font-size: 28px;
        color: #333;
        margin-left: 8px
      }
    }
    .convert-main {
      margin-top: 32px;
      color: #999;
      line-height: 140%;
    }

  }
}
.confirm-whole {
  width: 85%;
  border-radius: 16px;
  .convent-search {
    margin: 0;
    .list {
      color: #999;
      font-size: 24px;
      margin-top: 10px;
      line-height: 140%;
    }
    .reward {
      .title {
        font-size: 28px;
        font-weight: 500;
        color: #333;
        margin-top: 16px;
        text-align: left;
      }
      .tips {
        color: #999;
        font-size: 20px;
        line-height: 140%;
      }

      line-height: 140%;
    }
    .reward-list {
      display: flex;
      margin: 16px 0 40px 0;
      div {
        border-radius: 8px;
        margin-right: 16px;
        padding: 0px 16px;
        font-size: 24px;
        color: #fff;
        line-height: 60px;
        height: 60px;
        img {
          width: 32px;
          height: 32px!important;
          margin-right: 4px;
          position: relative;
          top: 6px;

        }
        &.point {
          background: #FF7500;
        }
        &.money {
          background: #FFC700;
        }
      }
    }
    .convert-btn {
      margin: 0;
    }
  }
}
</style>